<?php

namespace LowCode\Traits;

use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use LowCode\Core\Axios;
use LowCode\Core\Map;
use RuntimeException;

trait BasePageCache
{
    /**
     * 设置页面states数据
     * @param string $key
     * @param mixed $value
     * @param bool $isJSExpression
     * @return $this
     * @throws Exception
     */
    public function setPageState(string $key, mixed $value, bool $isJSExpression = false): static
    {
        $states = $this->getPageState();
        if ($states->get($key)){
//            throw new RuntimeException("Page state: ".$key." 已存在");
        }
        $content = ['key'=>$key,'value'=>$value,'isJSExpression'=>$isJSExpression];
        $states->put($key,$content);
        $this->pages->put('states',$states);
        return $this;
    }

    /**
     * 获取页面states数据
     * @return Collection
     */
    public function getPageState(): Collection
    {
        return $this->pages->get('states',Collection::make());
    }

    /**
     * 设置页面method方法
     * @param string $key 函数名
     * @param string $value 完整的函数
     * @return $this
     * @throws Exception
     */
    public function setPageMethod(string $key, string $value): static
    {
        $methods = $this->getPageMethod();
        if ($methods->get($key)){
            throw new Exception("Page method: ".$key." 已存在");
        }
        $methods->put($key,$value);
        $this->pages->put('methods',$methods);
        return $this;
    }

    /**
     * 获取页面methods数据
     * @return Collection
     */
    public function getPageMethod(): Collection
    {
        return $this->pages->get('methods',Collection::make());
    }

    /**
     * 设置page组件页面远程数据源
     * @param string $key 函数名
     * @param array|Axios $value
     * @return $this
     * @throws Exception
     */
    public function setPageDataSource(string $key, array|Axios $value): static
    {
        $sourceList = $this->getPageDataSource();
        if ($sourceList->get($key)){
            throw new Exception("Page sourceList: ".$key." 已存在");
        }
        $sourceList->put($key,$value);
        $this->pages->put('sourceList',$sourceList);
        return $this;
    }

    /**
     * 获取page组件页面远程数据源
     * @return Collection
     */
    public function getPageDataSource(): Collection
    {
        return $this->pages->get('sourceList',Collection::make());
    }

    /**
     * 设置组件映射
     * @param string $name
     * @param Map $map
     * @return $this
     * @throws Exception
     */
    public function setPageMaps(string $name, Map $map): static
    {
        $maps = $this->getPageMaps();
        if ($maps->get($name)){
            throw new Exception("组件映射的 maps: ".$name." 已存在");
        }
        $maps->put($name,$map);
        $this->pages->put('maps',$maps);
        return $this;
    }

    /**
     * 获取组件映射
     * @return Collection
     */
    public function getPageMaps(): Collection
    {
        return $this->pages->get('maps',Collection::make());
    }
}
